package com.lanou.dllo.shise.com.lanou.dllo.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.entity.FoodBean;
import com.lanou.dllo.shise.com.lanou.dllo.entity.MessageBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/3.
 */
public class MessageActivity extends Activity{
    private RequestQueue queue;
    private ImageView dessert_user_image, dessert_picture_url,
            japanese_cuisine_picture_url, japanese_cuisine_user_image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_japanese_cuisine);
        initView();
        initData();
    }

    private void initView() {
        queue = Volley.newRequestQueue(this);
        dessert_user_image = (ImageView) findViewById(R.id.dessert_user_image);
        dessert_picture_url = (ImageView) findViewById(R.id.dessert_picture_url);
        japanese_cuisine_user_image = (ImageView) findViewById(R.id.japanese_cuisine_user_image);
        japanese_cuisine_picture_url = (ImageView)findViewById(R.id.japanese_cuisine_picture_url);

    }
    private void initData() {
        String url = "http://api.meishixing.com/user/user/userbaseinfo/uid=23026&session_id=00001227993a5661b25da19c79c2a7c9a6affc";
         StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FoodBean foodBean = gson.fromJson(response, FoodBean.class);
                if (foodBean != null) {
                    FoodBean.ResultEntity food = foodBean.getResult();
                    if (food.getLast_picture_list().get(0).getUser_image() != null && food.getLast_picture_list().get(0).getUser_image().length() > 0) {
                        String url = food.getLast_picture_list().get(0).getUser_image();
                        getBitmap(url, dessert_user_image);
                    }
                    if (food.getLast_picture_list().get(1).getUser_image() != null && food.getLast_picture_list().get(1).getUser_image().length() > 0) {
                        String path = food.getLast_picture_list().get(1).getUser_image();
                        getBitmap(path, japanese_cuisine_user_image);
                    }
                    if (food.getLast_picture_list().get(0).getPicture_url() != null && food.getLast_picture_list().get(0).getPicture_url().length() > 0) {
                        String url = food.getLast_picture_list().get(0).getPicture_url();
                        getBitmap(url, dessert_picture_url);
                    }
                    if (food.getLast_picture_list().get(1).getPicture_url() != null && food.getLast_picture_list().get(1).getPicture_url().length() > 0) {
                        String path = food.getLast_picture_list().get(1).getPicture_url();
                        getBitmap(path, japanese_cuisine_picture_url);
                    }

                }

                    }


            }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(request);
    }


    private void getBitmap(String url, final ImageView fav_user_image) {
        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                dessert_picture_url.setImageBitmap(response);
            }
        }, 720, 360, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(imageRequest);
    }
    }



